﻿/*
Copyright (c) 2008, ThaoNguyen. All rights reserved.
*/
/**
 * The NTTHAO validator is a generic validator.
 * @module validator
 * @namespace NTTHAO.validator
 */
 
NTTHAO.validator = NTTHAO.validator || { };
//NTTHAO.namespace("validator");

/*
Check input field
@Params:
    1. type: name (in string) of validation type
    2. formField: control to check
@Return: true or false
@Note: "type" is one of these (case-sensitive):
    noblank,
    integer,
    decimal,
    text,
    alphanumeric,
    full,
    alphanum,
    verificationcode,
    email,
    phone,
    URL,
    path,
    image
*/
NTTHAO.validator.checkField = function(type, formField) {
	var checkOK;
	var checkStr = formField.value;
	var allValid = true;
	var flagDot  = false;
	var namestr, domainstr;
	
	if (type == 'noblank')
	{
		if (checkStr == "")
		{
			return false;
		}

        var i = 0;
	    for(i = 0; i < checkStr.length; i++) {
		    if ((checkStr.charAt(i) != ' ')
				    && (checkStr.charAt(i) != "\t")
				    && (checkStr.charAt(i) != "\n")
				    &&(checkStr.charAt(i) != "\r")) {
				break;
		    }
	    }
	    if (i == checkStr.length)
		{
			return false;
		}
	} 
	else 	
	{
		if (type == 'integer')	
		{
			checkOK = "0123456789+-";
			if(checkStr.length > 0) 
			{
			    if (!( /^[-+]?\b\d+\b$/.test(checkStr) ))
			    {
				    return false;
			    }
			}
		} 
		else if (type == 'decimal')
		{	
			checkOK = "0123456789.+-";
			if(checkStr.length > 0) 
			{ 
			    if (!( /^[-+]?((\b[0-9]+)?\.)?[0-9]+\b$/.test(checkStr) ))
			    {
				    return false;
			    }
			}
		} 
		else if (type == 'text') 
		{
			checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		} 
		else if (type == 'alphanumeric') 
		{
			checkOK = "0123456789.+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()_";
		} 
		else if (type == 'full') 
		{
			checkOK = "0123456789.,[]{}=+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()_:;'\\*^%$@<>?'";
		} 
		else if (type == 'alphanum') 
		{
			checkOK = "0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		} 
		else if (type == 'verificationcode') 
		{
			checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		} 
		else if (type == 'email')
		{
			checkOK = "0123456789_-@.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			if(checkStr.length > 0) 
			{
			    if (!( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/.test(checkStr) ))
			    {
				    return false;
			    }
			}
		} 
		else if (type == 'phone') 
		{
			checkOK = "0123456789-+";
		} 
		else if (type == 'URL') 
		{
			//checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.:/\\";
			var RegExp = /^(http|https|ftp)\:\/\/([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$/i;

            if(RegExp.test(checkStr)){
                return "";
            }else{
                return (strMsg  + String.fromCharCode(10));
            }
		} 
		else if (type == 'path') 
		{
			checkOK = "0123456789.+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz () \\ ";
		} 
		else if (type == 'image') 
		{
		    if (checkStr == "")
			    return true;
		    if (checkStr.lastIndexOf('.') < 0)
		        return false;
		    var imgEx = checkStr.substring(checkStr.lastIndexOf('.')).toLowerCase();
		    return (
		        (imgEx == '.png')
		        || (imgEx == '.jpg')
		        || (imgEx == '.jpeg')
		        || (imgEx == '.jpe')
		        || (imgEx == '.gif')
		        || (imgEx == '.bmp')
		        || (imgEx == '.tiff')
		        );
		}
		else 
		{
			return false;// "Check Validation one of the mentioned validation type is wrong";
		}
		
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++) 
			{
				if (ch == checkOK.charAt(j)) 
				{
					break; 
				}
				if (j == checkOK.length-1)
				{	
					allValid = false;
					break;
				}
			}
				
			if (!allValid)
			{
				return false;
			}
		}
		
		if (type == 'decimal') {/* for decimal type */
			for (t = 0;  t < checkStr.length;  t++)
			{	
				dot = checkStr.charAt(t);
				if (dot == '.' && flagDot == false) 
				{
					flagDot = true;
				} 
				else if (dot =='.' && flagDot == true)
				{
					return false;
				}
			}
		}
	}
	return true;
}

NTTHAO.validator.validate = function(val, args, type) {
    if(val) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if(args)
            args.IsValid = NTTHAO.validator.checkField(type, ctrlToValidate);
    }
}

NTTHAO.validator.validateWithNoBlank = function(val, args, type) {
    NTTHAO.validator.validate(val, args, 'noblank');
    if(args.IsValid) {
        NTTHAO.validator.validate(val, args, type);
    }
}

NTTHAO.validator.sameAs = function(val, args) {
    if(val) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        var ctrlSameAs = document.getElementById(ctrlToValidate.getAttribute('sameas'));
        if(args && ctrlSameAs) {
            args.IsValid = (ctrlToValidate.value == ctrlSameAs.value);
        }
    }
}

NTTHAO.validator.minMaxLengthRequired = function(val, args) {
    if(val) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        var minlength = ctrlToValidate.getAttribute('minlength');
        var maxlength = ctrlToValidate.getAttribute('maxlength');
        
        if(args && minlength && maxlength) {
            try{
                minlength = parseInt(minlength, 10);
                maxlength = parseInt(maxlength, 10);
                var valueLength = ctrlToValidate.value.trim().length;
                args.IsValid = ((valueLength >= minlength) && (valueLength <= maxlength));
            }catch(o){}
        }
    }
}

NTTHAO.validator.noBlank = function(val, args) {
    NTTHAO.validator.validate(val, args, 'noblank');
}

NTTHAO.validator.isInteger = function(val, args) {
    NTTHAO.validator.validate(val, args, 'integer');
}

NTTHAO.validator.isPositiveInteger = function(val, args) {
    NTTHAO.validator.validate(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseInt(ctrlToValidate.value, 10) > 0);
        }
    }
}

NTTHAO.validator.isNegativeInteger = function(val, args) {
    NTTHAO.validator.validate(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseInt(ctrlToValidate.value, 10) < 0);
        }
    }
}

NTTHAO.validator.isNaturalNumber = function(val, args) {
    NTTHAO.validator.validate(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseInt(ctrlToValidate.value, 10) >= 0);
        }
    }
}

NTTHAO.validator.isNaturalNumberVN = function(val, args) {
    var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
    var rawValue = ctrlToValidate.value;
    ctrlToValidate.value = ctrlToValidate.value.replace(/\./gi, '').replace(/,/gi, '.');
    NTTHAO.validator.validate(val, args, 'integer');
    if (args.IsValid) {
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseInt(ctrlToValidate.value, 10) >= 0);
        }
    }
    ctrlToValidate.value = rawValue;
}

NTTHAO.validator.isDecimal = function(val, args) {
    NTTHAO.validator.validate(val, args, 'decimal');
}

NTTHAO.validator.isPositiveDecimal = function(val, args) {
    NTTHAO.validator.validate(val, args, 'decimal');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseFloat(ctrlToValidate.value) > 0);
        }
    }
}

NTTHAO.validator.isNegativeDecimal = function(val, args) {
    NTTHAO.validator.validate(val, args, 'decimal');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        if (NTTHAO.validator.checkField('noblank', ctrlToValidate)) {
            args.IsValid = (parseFloat(ctrlToValidate.value) < 0);
        }
    }
}

NTTHAO.validator.isText = function(val, args) {
    NTTHAO.validator.validate(val, args, 'text');
}

NTTHAO.validator.isAlphanumeric = function(val, args) {
    NTTHAO.validator.validate(val, args, 'alphanumeric');
}

NTTHAO.validator.isAlphanum = function(val, args) {
    NTTHAO.validator.validate(val, args, 'alphanum');
}

NTTHAO.validator.isVerificationCode = function(val, args) {
    NTTHAO.validator.validate(val, args, 'verificationcode');
}

NTTHAO.validator.isEmail = function(val, args) {
    NTTHAO.validator.validate(val, args, 'email');
}

NTTHAO.validator.isPhone = function(val, args) {
    NTTHAO.validator.validate(val, args, 'phone');
}

NTTHAO.validator.isURL = function(val, args) {
    NTTHAO.validator.validate(val, args, 'URL');
}

NTTHAO.validator.isPath = function(val, args) {
    NTTHAO.validator.validate(val, args, 'path');
}

NTTHAO.validator.isImageFile = function(val, args) {
    NTTHAO.validator.validate(val, args, 'image');
}

NTTHAO.validator.isIntegerRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'integer');
}

NTTHAO.validator.isPositiveIntegerRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        args.IsValid = (parseInt(ctrlToValidate.value, 10) > 0);
    }
}

NTTHAO.validator.isNegativeIntegerRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        args.IsValid = (parseInt(ctrlToValidate.value, 10) < 0);
    }
}

NTTHAO.validator.isNaturalNumberRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'integer');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        args.IsValid = (parseInt(ctrlToValidate.value, 10) >= 0);
    }
}

NTTHAO.validator.isNaturalNumberVNRequired = function(val, args) {
    var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
    var rawValue = ctrlToValidate.value;
    ctrlToValidate.value = ctrlToValidate.value.replace(/\./gi, '').replace(/,/gi, '.');
    NTTHAO.validator.validateWithNoBlank(val, args, 'integer');
    if (args.IsValid) {
        args.IsValid = (parseInt(ctrlToValidate.value, 10) >= 0);
    }
    ctrlToValidate.value = rawValue;
}

NTTHAO.validator.isDecimalRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'decimal');
}

NTTHAO.validator.isPositiveDecimalRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'decimal');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        args.IsValid = (parseFloat(ctrlToValidate.value) > 0);
    }
}

NTTHAO.validator.isNegativeDecimalRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'decimal');
    if (args.IsValid) {
        var ctrlToValidate = document.all ? document.all[val.controltovalidate] : document.getElementById(val.controltovalidate);
        args.IsValid = (parseFloat(ctrlToValidate.value) < 0);
    }
}

NTTHAO.validator.isTextRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'text');
}

NTTHAO.validator.isAlphanumericRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'alphanumeric');
}

NTTHAO.validator.isAlphanumRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'alphanum');
}

NTTHAO.validator.isVerificationCodeRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'verificationcode');
}

NTTHAO.validator.isEmailRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'email');
}

NTTHAO.validator.isPhoneRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'phone');
}

NTTHAO.validator.isURLRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'URL');
}

NTTHAO.validator.isPathRequired = function(val, args) {
    NTTHAO.validator.validateWithNoBlank(val, args, 'path');
}

NTTHAO.validator.validateMe = function() {
    // Clean Up Infragistics Ids
    var cleanid = this.id.replace(/^igtxt/i,"");

    for (var i = 0; i < Page_Validators.length; i++) {
        if (Page_Validators[i].controltovalidate === cleanid) {
            ValidatorValidate(Page_Validators[i]);
        }
    }
}